package com.yoso;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.time.ZoneId;
import java.util.Date;

@Slf4j
public class ListNode {

    /**
     * 1.TM3=2024/1/25 today=2024/1/25 则距离锁定天数为1
     * 2.TM3=2024/1/25 today=2024/1/24 则距离锁定天数为2
     * 3.TM3=2024/1/25 today=2024/1/26则距离锁定天数为0
     * 4.TM3=2024/1/25 today=2024/1/27则距离锁定天数为-1
     * @author shizejin
     * @date 2024/10/22 13:34
     */
    @Test
    public void ff2(){
        DateTime theLastedDate = DateUtil.parse("2024-07-26 14:59:32");
        DateTime referDate = DateUtil.parse("2024/10/26");

        int offLineDay = calculateRemainingLockDay(referDate, theLastedDate, 90);
        System.out.println("offLineDay = " + offLineDay);
    }

    /**
     * 计算剩余锁定天数
     *
     * @param referDate         参照日期，默认为当天
     * @param theLastedDate     最后一次操作时间
     * @param userMaxOffLineDay 最大离线天数
     * @return long
     * @author shizejin
     * @date 2024/10/22 13:13
     */
    private Integer calculateRemainingLockDay(Date referDate, Date theLastedDate, int userMaxOffLineDay) {
        userMaxOffLineDay = userMaxOffLineDay < 0 ? 1 : userMaxOffLineDay;
        referDate = referDate == null ? DateUtil.date() : referDate;
        // 锁定的日期
        Date lockDate = DateUtil.offsetDay(theLastedDate, userMaxOffLineDay);
        log.info("锁定的日期：{}", DateUtil.formatDate(lockDate));

        long between = DateUtil.between(DateUtil.beginOfDay(referDate), DateUtil.beginOfDay(lockDate),
                DateUnit.DAY, false);
        int intExact = Math.toIntExact(between + 1);
        if (intExact <= -1) {
            intExact = -1;
        }
        return intExact;
    }
}
